var startHint;
var recordFlag:Boolean;
var textLayer;
var micStatus:String;
// var prevBlockFlag:Boolean;
var onceFlag:Boolean;
var recordedFlag:Boolean;

function init()
{
	trace(this._name+" init...");

	var cont:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "processResult06");
	Packages.components.ReadingRecorder.refreshResultContent(cont, false);
	var hlp:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "recordHelpContent04");
	Packages.components.ReadingRecorder.refreshHelpContent(hlp);
	recordedFlag=false;
	onceFlag=false;
	micStatus="silence";
	Packages.task.Processor.clear();

	recordFlag=false;
	startHint=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "startHint");
	var portlet=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "txtReader");
	Packages.modula.TxtReader.getInstance(portlet);

	Packages.modula.TxtReader.displayBlockProgress();

	var nc=mlf.mcmanager.MessageAgent.getMessageSlot("mlf", "defaultConnection");
	Packages.modula.MicrophoneDevice.initMicDevice(nc, "Stat");
}

function nextBlock(preOffsetFlag:Boolean)
{
	if (recordFlag) 
	{	
		// prevBlockFlag=true;
		
		if (onceFlag) Packages.modula.TxtReader.switchBlock(1, true, preOffsetFlag);
		else 
		{
			onceFlag=true;
			Packages.modula.TxtReader.switchBlock(-2, true);
		}
		micStatus="active";
		activate();
	}
	else Packages.modula.TxtReader.switchBlock(1, recordFlag);
}

function prevBlock()
{
	if (recordFlag)		// && prevBlockFlag 
	{
		// prevBlockFlag=false;
		
		Packages.modula.TxtReader.switchBlock(-1, true);
		micStatus="active";
		activate();
	}
	else Packages.modula.TxtReader.switchBlock(-1, recordFlag);
}

function start()
{
	trace("start: "+recordFlag);
	recordFlag=!recordFlag;

	if (recordedFlag && !recordFlag)
	{
		var cont:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "processResult08");
		Packages.components.ReadingRecorder.refreshResultContent(cont, true);
	}
	if (recordFlag) startHint.htmlText=Packages.system.MLF.scanDictionary(startHint, "Stop Rec");
	else startHint.htmlText=Packages.system.MLF.scanDictionary(startHint, "Start Rec");

	// addMicrophoneActivityEventListener (filtered by MicrophoneDevice)
	// silence, active
	
	if (recordFlag)
	{
		// set textLayer
		var portlet=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "txtReader");
		textLayer=portlet.filling_rect.textLayer;
		// publish and first block
		microphoneStatus("active");
		// nextBlock();
		recordedFlag=true;
		var cont:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "processResult07");
		Packages.components.ReadingRecorder.refreshResultContent(cont, false);
	}
	else 
	{
		micStatus="silence";
	}

	Packages.task.Processor.record(recordFlag);
}

function microphoneStatus(param)
{
	trace("microphoneStatus: "+param);
	if (!recordFlag) return;
	if (param.indexOf("silence")>-1)
	{
		// silence status
		if (micStatus=="active")	// "active" means the voice launched
		{
			// check progress threshold
			if (overGap()) 
			{
				micStatus="silence";	// "silence" means the voice end
				silence();
			}
		}
	}
	else if (param.indexOf("active")>-1)
	{
		// active status
		if (micStatus=="silence")
		{
			// auto change
			micStatus="active";
			activate();
			nextBlock(true);	//
		}
	}
	trace("micStatus: "+micStatus);
}

function overGap():Boolean
{
	if (textLayer.textColor==0x000000)
	{
		return true;
	}
	else return false;
}
/*
function startRecord(uri:String)
{
	trace("start record...");
	Packages.modula.MicrophoneDevice.publish(uri, "record");
}

function pauseRecord(uri:String)
{
	trace("pause record...");
	Packages.modula.MicrophoneDevice.pause(uri);
}

function resumeRecord(uri:String)
{
	trace("resume record...");
	Packages.modula.MicrophoneDevice.resume(uri);
}
*/
function silence()
{
	trace("silence...");
	var ladder=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "aladder");
	var iconOn=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "actOnIcon");
	ladder._visible=false;
	iconOn._visible=false;
}

function activate()
{
	trace("activate...");
	var ladder=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "aladder");
	var iconOn=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "actOnIcon");
	ladder._visible=true;
	iconOn._visible=true;
}

function ladderChange(activityLevel:Number)
{
	//trace("ladderChange...activityLevel:"+activityLevel+" mode:"+mode);
	var ladder=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "aladder");
	ladder.jnladder.pos=activityLevel;
	ladder.jnladder.draw();		
}
/*
function finish(param, data, slot)
{
	Packages.modula.TxtReader.removeInstance();
	Packages.components.Scene.enterPostProduction(param, data, slot);
}*/

function rolloutCon(param, data, slot)
{
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=true;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 0);
}

function rolloverCon(param, data, slot)
{
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=false;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 50);
}

function pressCon(param, data, slot)
{
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, "highlightBg"), 100);
}

function releaseCon(param, data, slot)
{
	rolloverCon(param, data, slot);
	if (param.indexOf("nextblock")>-1) nextBlock();
	else if (param.indexOf("prevblock")>-1) prevBlock();
	else if (param.indexOf("start")>-1) start();
}